import os

from multiprocessing import Manager, Lock

import lib, sql

from xdh_logging import initLog

log = initLog("playfile")

mgr = Manager()

class PlayFile(object):
	used_tracks = mgr.list()
	track_lock = Lock()

	@classmethod
	def track_ok(cls, track):
		ret = False
		with cls.track_lock:
			if not track in cls.used_tracks:
				cls.used_tracks.append(track)
				ret = True
		return ret


	@classmethod
	def clear_used_tracks(cls):
		with cls.track_lock:
			del cls.used_tracks[:]


	def __init__(self):
		pass

	@property
	def playlist(self):
		ret = set([filename for filename in sql.SubData.getPlaylist()])
		ret |= set([filename for filename in sql.HolidayData.getPlaylist()])
		return ret


	def remove(self, tracks):
		sub_dels = []
		hol_dels = []

		for filename in tracks:
			if sql.SubData.contains(filename):
				sub_dels.append({"filename": filename})
			elif sql.HolidayData.contains(filename):
				hol_dels.append({"filename": filename})

		if len(sub_dels) > 0:
			sql.process(sql.SubData, "delete", sub_dels)

		if len(hol_dels) > 0:
			sql.process(sql.HolidayData, "delete", hol_dels)
